<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改单词信息管理')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-wordinfo-edit" th:object="${word}">
            <h4 class="form-header h4">单词信息管理信息</h4>
            <input name="id" th:field="*{id}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">单词外文：</label>
                    <div class="col-sm-8">
                        <input name="name" th:field="*{name}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">中文翻译：</label>
                    <div class="col-sm-8">
                        <input name="meaning" th:field="*{meaning}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">词源：</label>
                    <div class="col-sm-8">
                        <input name="etymology" th:field="*{etymology}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">词性：</label>
                    <div class="col-sm-8">
                        <input name="partOfSpeech" th:field="*{partOfSpeech}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">图片：</label>
                    <div class="col-sm-8">
                        <input type="hidden" id="imageField" name="image" th:field="*{image}">
                       <div class="file-loading">
                            <input class="form-control file-upload" id="image" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">语音：</label>
                    <div class="col-sm-8">
                        <input type="hidden" id="soundField" name="sound" th:field="*{sound}">
                        <div class="file-loading">
                            <input class="form-control file-upload" id="sound" name="file" type="file">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">可用状态：</label>
                    <div class="col-sm-8">
                        <select name="status" class="form-control" th:with="type=${@dict.getType('word_status')}" required>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{status}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">单词分组信息</label>
                    <div class="col-sm-8">
                        <div th:with="wordType=${@wordDict.getType()}" class="border p-3 rounded">
                            <div class="form-check" th:each="wordDict : ${wordType}">
                                <input class="form-check-input" type="checkbox" th:id="'group'+${wordDict.id}" th:name="'groupId'" th:value="${wordDict.id}" th:checked="${#lists.contains(word.wordGroupList.![groupId], wordDict.id)}">
                                <label class="form-check-label" th:for="'group'+${wordDict.id}" th:text="${wordDict.value}"></label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "teacher/wordinfo";
        $("#form-wordinfo-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-wordinfo-edit').serialize());
            }
        }

        $(".file-upload").each(function (i) {
            var hiddenFieldId = this.id + "Field";
            var val = $("#" + hiddenFieldId).val();
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("#" + event.currentTarget.id + "Field").val(data.response.url);
                event.stopPropagation();
                return false;
            }).on('fileremoved', function (event, id, index) {
                $("#" + event.currentTarget.id + "Field").val('')
                event.stopPropagation();
                return false;
            });
            $(this).fileinput('_initFileActions');
        });

        $(function() {
            var options = {
                data: [[${word.wordGroupList}]],
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'name',
                    align: 'center',
                    title: '分组类别',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='wordGroupList[%s].name' value='%s'>", index, value);
                        return html;
                    }
                },

                {
                    field: 'value',
                    align: 'center',
                    title: '分组详情',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='wordGroupList[%s].value' value='%s'>", index, value);
                        return html;
                    }
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
            };
            $.table.init(options);
        });

        function addRow() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                name: "",
                value: "",
            }
            sub.addRow(row);
        }
    </script>
</body>
</html>